package cn.passlink.bletoothchat.SqlTables;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.passlink.bletoothchat.Entity.Friends;

public class FriendsTableManager {

    private SQLiteDatabase database;

    public FriendsTableManager(SQLiteDatabase database) {
        this.database = database;
    }

    public void addFriend(Friends friend) {
        ContentValues values = new ContentValues();
        values.put("phone", friend.getPhone());
        values.put("username", friend.getUsername());
        values.put("academy", friend.getAcademy());
        values.put("grade", friend.getGrade());
        values.put("classes", friend.getClasses());
        values.put("area", friend.getArea());
        values.put("position", friend.getPosition());
        values.put("change_numbers", friend.getChange_numbers());
        values.put("groups_id", friend.getGroups_id());
        values.put("friend_id", friend.getFriend_id());

        database.insert("friends", null, values);
    }

    public void deleteFriend(int friendId) {
        String whereClause = "id" + " = ?";
        String[] whereArgs = {String.valueOf(friendId)};
        database.delete("friends", whereClause, whereArgs);
    }

    public void updateFriend(Friends friend) {
        ContentValues values = new ContentValues();
        values.put("phone", friend.getPhone());
        values.put("username", friend.getUsername());
        values.put("academy", friend.getAcademy());
        values.put("grade", friend.getGrade());
        values.put("classes", friend.getClasses());
        values.put("area", friend.getArea());
        values.put("position", friend.getPosition());
        values.put("change_numbers", friend.getChange_numbers());
        values.put("groups_id", friend.getGroups_id());
        values.put("friend_id", friend.getFriend_id());

        String whereClause = "phone" + " = ?";
        String[] whereArgs = {String.valueOf(friend.getPhone())};
        database.update("friends", values, whereClause, whereArgs);
    }

    public Friends getFriendById(int friendId) {
        String[] projection = {"id", "phone", "username", "academy", "grade", "classes","area", "position", "change_numbers", "groups_id", "friend_id"};
        String selection = "id" + " = ?";
        String[] selectionArgs = {String.valueOf(friendId)};

        Cursor cursor = database.query("friends", projection, selection, selectionArgs, null, null, null);
        if (cursor.moveToFirst()) {
            Friends friend = new Friends();

            friend.setPhone(cursor.getString(cursor.getColumnIndexOrThrow("phone")));
            friend.setUsername(cursor.getString(cursor.getColumnIndexOrThrow("username")));
            friend.setAcademy(cursor.getString(cursor.getColumnIndexOrThrow("academy")));
            friend.setGrade(cursor.getString(cursor.getColumnIndexOrThrow("grade")));
            friend.setClasses(cursor.getString(cursor.getColumnIndexOrThrow("classes")));
            friend.setArea(cursor.getString(cursor.getColumnIndexOrThrow("area")));
            friend.setPosition(cursor.getString(cursor.getColumnIndexOrThrow("position")));
            friend.setChange_numbers(cursor.getString(cursor.getColumnIndexOrThrow("change_numbers")));
            friend.setGroups_id(cursor.getString(cursor.getColumnIndexOrThrow("groups_id")));
            friend.setFriend_id(cursor.getString(cursor.getColumnIndexOrThrow("friend_id")));

            cursor.close();
            return friend;
        }

        cursor.close();
        return null;
    }


    public List<Friends> getAllFriends() {
        List<Friends> friendsList = new ArrayList<>();

        String[] projection = {"id", "phone", "username", "academy", "grade", "classes","area", "position", "change_numbers", "groups_id", "friend_id"};

        Cursor cursor = database.query("friends", projection, null, null, null, null, null);
        while (cursor.moveToNext()) {
            Friends friend = new Friends();
            friend.setPhone(cursor.getString(cursor.getColumnIndexOrThrow("phone")));
            friend.setUsername(cursor.getString(cursor.getColumnIndexOrThrow("username")));
            friend.setAcademy(cursor.getString(cursor.getColumnIndexOrThrow("academy")));
            friend.setGrade(cursor.getString(cursor.getColumnIndexOrThrow("grade")));
            friend.setClasses(cursor.getString(cursor.getColumnIndexOrThrow("classes")));
            friend.setArea(cursor.getString(cursor.getColumnIndexOrThrow("area")));
            friend.setPosition(cursor.getString(cursor.getColumnIndexOrThrow("position")));
            friend.setChange_numbers(cursor.getString(cursor.getColumnIndexOrThrow("change_numbers")));
            friend.setGroups_id(cursor.getString(cursor.getColumnIndexOrThrow("groups_id")));
            friend.setFriend_id(cursor.getString(cursor.getColumnIndexOrThrow("friend_id")));

            friendsList.add(friend);
        }

        cursor.close();
        return friendsList;
    }
}